package com.scaffold.authority.service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.sql.Timestamp;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import wangjg.commons.page.Page;

import com.scaffold.authority.dao.AuthorityDAO;
import com.scaffold.authority.dao.ResourceAuthorityRelationDAO;
import com.scaffold.authority.dao.ResourceDAO;
import com.scaffold.authority.entity.Authority;
import com.scaffold.authority.entity.Resource;
import com.scaffold.authority.entity.ResourceAuthorityRelation;

@Service
@Transactional
public class ResourceService {

	@Autowired
	private ResourceDAO resourceDAO;
	@Autowired
	private ResourceAuthorityRelationDAO resourceAuthorityRelationDAO;
	@Autowired
	private AuthorityDAO authorityDAO;

	public Resource getResource(Long id ) {
		return this.resourceDAO.findById(id);
	}

	public void save(Resource resource){
		Timestamp now = new Timestamp(System.currentTimeMillis());
		if(resource.getId()==null){
			resource.setCreated(now);
			resource.setModified(now);
			this.resourceDAO.save(resource);
		}else{
			resource.setModified(now);
			this.resourceDAO.update(resource);
		}
	}

	public void update(Resource resource){
		Timestamp now = new Timestamp(System.currentTimeMillis());
		resource.setModified(now);
		this.resourceDAO.update(resource);
	}

	/**
	 * @param idseq
	 */
	public void batchDelete(String idseq) {
		String[] sa = idseq.split(",");
		for(String s:sa){
			Long id = Long.parseLong(s);
			this.resourceAuthorityRelationDAO.deleteByProperty("resourceId", id);
			this.resourceDAO.delete(id);
		}
	}

	public void delete(Resource resource){
		this.resourceAuthorityRelationDAO.deleteByProperty("resourceId", resource.getId());
		this.resourceDAO.delete(resource);
	}
	
	public List<Resource> pageQuery(Page page , Integer type, String value, String keyword) {
		return this.resourceDAO.pageQuery(page, value, type, keyword);
	}

	public void saveAuthoritys(Long resourceId, String authorityIds) {
		resourceAuthorityRelationDAO.deleteByProperty("resourceId", resourceId);
		
		if(resourceId==null || authorityIds==null || authorityIds.isEmpty()){
			return;
		}
		String[] idseq = authorityIds.split(",");
		Timestamp now = new Timestamp(System.currentTimeMillis());
		for(String s:idseq){
			Long authorityId = Long.parseLong(s);
			ResourceAuthorityRelation item = new ResourceAuthorityRelation();
			item.setResourceId(resourceId);
			item.setAuthorityId(authorityId);
			item.setCreated(now);
			item.setModified(now);
			resourceAuthorityRelationDAO.save(item);
		}
	}

	public List<Long> getAuthorityIdList(Long resourceId) {
		return resourceAuthorityRelationDAO.findPropertyList("authorityId", "resourceId", resourceId);
	}

	public List<Authority> getAuthorityList(Long resourceId) {
		List idList =  resourceAuthorityRelationDAO.findPropertyList("authorityId", "resourceId", resourceId);
		return authorityDAO.findByIdList(idList);
	}

	public List<Resource> findAll() {
		List<Resource> list = this.resourceDAO.pageQuery(null);
		for(Resource res:list){
			List<Authority> alist = this.getAuthorityList(res.getId());
			res.setAuthorities(alist);
		}
		return list;
	}

	/**
	 * @param text
	 * @throws IOException 
	 */
	public void imp(String text) throws IOException {
		StringReader sr = new StringReader(text);
		BufferedReader br = new BufferedReader(sr);
		String line = null;
		String tag = null;
		while((line=br.readLine())!=null){
			if(line.startsWith("#")){
				tag = line.substring(1).trim();
				continue;
			}
			String[] sa = line.split("[\\t ]+");
			Resource resource = new Resource();
			String url = sa[0];
			url = url.replaceAll("\"", "");
			url = url.replaceAll("\\{\\w*\\}", "*");
			resource.setValue(url);
			resource.setName(sa[1]);
			if(sa.length>2){
				resource.setType(Integer.parseInt(sa[2]));
			}
			resource.setTag(tag);
			this.save(resource);
		}
	}
}
